package com.gzw.dms.activity;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gzw.dms.KJ.*;
import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.ShippingOrderInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.bean.WaybillAdjustmentHistoryInfo;
import com.gzw.dms.bean.WaybillAdjustmentOrderInfo;
import com.gzw.dms.utils.FileUtil;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class OrderChangeDetailActivity extends BaseActivity {

    private Context mContext;
    private DatePickerDialog dpd;
    private ProgressDialog mProgressDialog;
    private String costomersid,user_id;
    private Button btn_sign;
    private Intent mIntent;
    private Toolbar toolbar;
    private EditText et_order_num,et_sign_startdress,et_custom_name,
            et_custom_contact,et_custom_tel,et_sign_goodsName,et_sign_goodsNumber,
            et_sign_goodsWeight,et_sign_goodsTiji,et_shopping_order_num,et_send_mechanism,
            et_receipt, et_receipt_name,et_receipt_tel,et_deliver_fee,et_upstairs_fee,et_added_fee,et_other_fee,
    et_trade_agency,et_is_recept,et_sign_remark;
    private TextView tv_send_time,et_sign_endadress;
    private ShippingOrderInfo info;
    private  String fpstste="0";
    private String adress,driver_province,driver_city,driver_county,fhku_id;
    private WaybillAdjustmentHistoryInfo waybillAdjustmentHistoryInfo;
    private List<WaybillAdjustmentHistoryInfo> infoList = new ArrayList<>();
    private String applicant_message="";
    private String shopping_num = "";
    private LinearLayout layout_change;
    private LinearLayout layout_str;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_order_change_detail);

    }

    @Override
    protected void initViews() {


        btn_sign = ((Button) findViewById(R.id.btn_sign));
        toolbar = (Toolbar) findViewById(R.id.toolbar);

        et_order_num = ((EditText) findViewById(R.id.et_order_num));
        et_sign_startdress = ((EditText) findViewById(R.id.et_sign_startdress));
        et_sign_endadress = ((TextView) findViewById(R.id.et_sign_endadress));
        et_custom_name = ((EditText) findViewById(R.id.et_custom_name));
        et_custom_contact = ((EditText) findViewById(R.id.et_custom_contact));
        et_sign_goodsName = ((EditText) findViewById(R.id.et_sign_goodsName));
        et_custom_tel = ((EditText) findViewById(R.id.et_custom_tel));
        et_sign_goodsNumber = ((EditText) findViewById(R.id.et_sign_goodsNumber));
        et_sign_goodsWeight = ((EditText) findViewById(R.id.et_sign_goodsWeight));
        et_sign_goodsWeight.setInputType(InputType.TYPE_CLASS_PHONE);
        et_sign_goodsTiji = ((EditText) findViewById(R.id.et_sign_goodsTiji));
        et_sign_goodsTiji.setInputType(InputType.TYPE_CLASS_PHONE);
        et_sign_remark = ((EditText) findViewById(R.id.et_sign_remark));
        tv_send_time = (TextView) findViewById(R.id.order_time);
        et_shopping_order_num = ((EditText) findViewById(R.id.et_shopping_order_num));
        et_send_mechanism = ((EditText) findViewById(R.id.et_send_mechanism));
        et_receipt = ((EditText) findViewById(R.id.et_receipt));
        et_receipt_name = ((EditText) findViewById(R.id.et_receipt_name));
        et_receipt_tel = ((EditText) findViewById(R.id.et_receipt_tel));
        et_deliver_fee = ((EditText) findViewById(R.id.et_deliver_fee));
        et_upstairs_fee = ((EditText) findViewById(R.id.et_upstairs_fee));
        et_added_fee = ((EditText) findViewById(R.id.et_added_fee));
        et_other_fee = ((EditText) findViewById(R.id.et_other_fee));
        et_trade_agency = ((EditText) findViewById(R.id.et_trade_agency));
        et_is_recept = ((EditText) findViewById(R.id.et_is_recept));
        layout_change = ((LinearLayout) findViewById(R.id.layout_change));
        layout_str = ((LinearLayout) findViewById(R.id.layout_str));

        //et_shopping_order_num,et_send_mechanism,
//        et_receipt, et_receipt_name,et_receipt_tel,et_deliver_fee,et_upstairs_fee,et_added_fee,et_other_fee,
//                et_trade_agency,et_is_recept,et_sign_remark

    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("运单调整详情");
        mIntent = getIntent();
        info = (ShippingOrderInfo) mIntent.getExtras().get(
                "shippingOrder");
        fpstste=(String)mIntent.getExtras().get("fpstste");
        shopping_num = info.getShiping_order_num();
        if (info.getWaybill_state()==1 || info.getWaybill_state()==2){
            layout_change.setVisibility(View.GONE);
            layout_str.setVisibility(View.VISIBLE);
        }else {
            layout_str.setVisibility(View.GONE);

        }
        setData(info);

    }

    @Override
    protected void initEvents() {
        btn_sign.setOnClickListener(new myOnClick());
        toolbar.setNavigationOnClickListener(new myOnClick());
        tv_send_time.setOnClickListener(new myOnClick());
        et_sign_endadress.setOnClickListener(new myOnClick());
    }
    private void setData(ShippingOrderInfo shippingOrderInfo) {
        et_shopping_order_num.setHint(shippingOrderInfo.getShiping_order_num());
//        et_shopping_order_num.setText(shippingOrderInfo.getShiping_order_num());
        et_order_num.setHint(shippingOrderInfo.getOrder_number());
        et_sign_startdress.setHint(shippingOrderInfo.getSend_station());
        et_sign_endadress.setText(shippingOrderInfo.getEnd_address());
        et_custom_name.setHint(shippingOrderInfo.getCustom_name());
        et_custom_contact.setHint(shippingOrderInfo.getCustom_contact());
        et_custom_tel.setHint(shippingOrderInfo.getCustom_tel());
        et_sign_goodsName.setHint(shippingOrderInfo.getGoods_name());
        et_sign_goodsNumber.setHint(shippingOrderInfo.getGoods_num().toString());
        et_sign_goodsTiji.setHint(shippingOrderInfo.getGoods_vol());
        et_sign_goodsWeight.setHint(shippingOrderInfo.getGoods_weight());
        tv_send_time.setText(shippingOrderInfo.getSend_time());
        et_send_mechanism.setHint(shippingOrderInfo.getSend_mechanism());
        et_receipt.setHint(shippingOrderInfo.getReceipt());
        et_receipt_name.setHint(shippingOrderInfo.getReceipt_name());
        et_receipt_tel.setHint(shippingOrderInfo.getReceipt_tel());
        et_added_fee.setHint(shippingOrderInfo.getAdded_fee());
        et_deliver_fee.setHint(shippingOrderInfo.getDeliver_fee());
        et_other_fee.setHint(shippingOrderInfo.getOther_fee());
        et_upstairs_fee.setHint(shippingOrderInfo.getUpstairs_fee());
        et_trade_agency.setHint(shippingOrderInfo.getTrade_agency());
        et_is_recept.setHint(shippingOrderInfo.getIs_recept());




    }
    class myOnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.btn_sign:
                    SaveHttpClientPost();
                    break;
                case R.id.order_time:
                    Calendar mCalendar = Calendar.getInstance(Locale.CHINA);
                    int year = mCalendar.get(Calendar.YEAR);
                    int month = mCalendar.get(Calendar.MONTH);
                    int day = mCalendar.get(Calendar.DAY_OF_MONTH);
                    dpd = new DatePickerDialog(mContext, new Datelistener(), year, month,
                            day);
                    dpd.show();
                    break;

                case R.id.et_sign_endadress:
                    Intent mIntent1 = new Intent();
                    mIntent1.putExtra("who", "0");
                    mIntent1.setClass(mContext, SelectCityActivity.class);
                    startActivityForResult(mIntent1, 2);
                    break;
                default:
                    finish();
                    break;
            }
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 0){
            try {
//                ArrayList<String> name1 = data.getStringArrayListExtra("name");
//                String name2 = name1.toString();
//                String name = name2.substring(1,name2.length()-1);
                String name = data.getStringExtra("name");
                String id = data.getStringExtra("id");

            } catch (Exception e) {
                // TODO: handle exception
            }
        }else if (resultCode == 2){
            try {
                String province = data.getStringExtra("province");
                String city = data.getStringExtra("city");
                String county = data.getStringExtra("county");
                driver_province = data.getStringExtra("province_id");
                driver_city = data.getStringExtra("city_id");
                String county_id = data.getStringExtra("county_id");
                String str2 = county.substring(1, county.length()-1);
                String str = province+city+str2;
                adress=str;
                et_sign_endadress.setText(str);
                driver_county = county_id.substring(1,county_id.length()-1);

            } catch (Exception e) {
                // TODO: handle exception
            }
        }else if (resultCode == 3){
            try {
                String name = data.getStringExtra("name");
                String id = data.getStringExtra("id");


            } catch (Exception e) {
                // TODO: handle exception
            }

        }
    }
    class Datelistener implements DatePickerDialog.OnDateSetListener {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {

            // TODO Auto-generated method stub
            tv_send_time.setText(year + "-" + (monthOfYear + 1) + "-"
                    + dayOfMonth);
        }
    }
    public void SaveHttpClientPost() {
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//        String date=sdf.format(new java.util.Date());
//        tv_send_time.setText(date);
        if (isNull(et_order_num)) {

        }else {
            String title = "订单号";
            change(title,et_order_num.getText().toString().trim(),info.getOrder_number(),title);
            info.setOrder_number(et_order_num.getText().toString().trim());
        }
        if (isNull(et_shopping_order_num)) {
        }else {
            String title = "货运编号";
            change(title, et_shopping_order_num.getText().toString().trim(), info.getShiping_order_num(), title);
            info.setShiping_order_num(et_shopping_order_num.getText().toString().trim());
        }
        if (isNull(et_sign_startdress)) {
        }else {
            String title = "起运地";
            change(title, et_sign_startdress.getText().toString().trim(), info.getSend_station(), title);
            info.setSend_station(et_sign_startdress.getText().toString().trim());
        }
        if (et_sign_endadress.getText().toString().trim().equals(info.getEnd_address())) {
        }else {
            String title = "终到位置";
            change(title, et_sign_endadress.getText().toString().trim(), info.getEnd_address(), title);
            info.setEnd_address(et_sign_endadress.getText().toString().trim());
        }

        String time = tv_send_time.getText().toString().trim();
        String time1 = info.getSend_time();

        if (tv_send_time.getText().toString().trim().equals(info.getSend_time())) {

        }else {
            String title = "起运时间";
            change(title, tv_send_time.getText().toString().trim(), info.getSend_time(), title);
            info.setSend_time(tv_send_time.getText().toString().trim());
        }
        if (isNull(et_custom_name)) {
        }else {
            String title = "发货客户名称";
            change(title, et_custom_name.getText().toString().trim(), info.getCustom_name(), title);
            info.setCustom_name(et_custom_name.getText().toString().trim());
        }
        if (isNull(et_custom_contact)) {
        }else {
            String title = "发货客户联系人";
            change(title, et_custom_contact.getText().toString().trim(), info.getCustom_contact(), title);
            info.setCustom_contact(et_custom_contact.getText().toString().trim());
        }
        if (isNull(et_custom_tel)) {
        }else {
            String title = "发货客户联系电话";
            change(title, et_custom_tel.getText().toString().trim(), info.getCustom_tel(), title);
            info.setCustom_tel(et_custom_tel.getText().toString().trim());
        }
        if (isNull(et_sign_goodsName)) {
        }else {
            String title = "货物名称";
            change(title, et_sign_goodsName.getText().toString().trim(), info.getGoods_name(), title);
            info.setGoods_name(et_sign_goodsName.getText().toString().trim());
        }
        if (isNull(et_sign_goodsNumber)) {
        }else {
            String title = "总件数";
            change(title, et_sign_goodsNumber.getText().toString().trim(), info.getGoods_num().toString(), title);
            int num = Integer.parseInt(et_sign_goodsNumber.getText().toString().trim());
            info.setGoods_num(num);
        }
        if (isNull(et_sign_goodsTiji)) {
        }else {
            String title = "总体积";
            change(title, et_sign_goodsTiji.getText().toString().trim(), info.getGoods_vol(), title);
            info.setGoods_vol(et_sign_goodsTiji.getText().toString().trim());
        }
        if (isNull(et_sign_goodsWeight)) {
        }else {
            String title = "总质量";
            change(title, et_sign_goodsWeight.getText().toString().trim(), info.getGoods_weight(), title);
            info.setGoods_weight(et_sign_goodsWeight.getText().toString().trim());
        }
        if (isNull(et_send_mechanism)) {
        }else {
            String title = "受理机构";
            change(title, et_send_mechanism.getText().toString().trim(), info.getSend_mechanism(), title);
            info.setSend_mechanism( et_send_mechanism.getText().toString().trim());
        }
        if (isNull(et_receipt)) {
        }else {
            String title = "收获客户名称";
            change(title, et_receipt.getText().toString().trim(), info.getReceipt(), title);
            info.setReceipt(et_receipt.getText().toString().trim());
        }
        if (isNull(et_receipt_name)) {
        }else {
            String title = "到货联系人";
            change(title, et_receipt_name.getText().toString().trim(), info.getReceipt_name(), title);
            info.setReceipt_name(et_receipt_name.getText().toString().trim());
        }
        if (isNull(et_receipt_tel)) {
        }else {
            String title = "到货联系电话";
            change(title, et_receipt_tel.getText().toString().trim(), info.getReceipt_tel(), title);
            info.setReceipt_tel(et_receipt_tel.getText().toString().trim());
        }
        if (isNull(et_upstairs_fee)) {
        }else {
            String title = "上楼费";
            change(title, et_upstairs_fee.getText().toString().trim(), info.getUpstairs_fee(), title);
            info.setUpstairs_fee(et_upstairs_fee.getText().toString().trim());
        }
        if (isNull(et_added_fee)) {
        }else {
            String title = "附加费";
            change(title, et_added_fee.getText().toString().trim(), info.getAdded_fee(), title);
            info.setAdded_fee(et_added_fee.getText().toString().trim());
        }
        if (isNull(et_deliver_fee)) {
        }else {
            String title = "配送费";
            change(title, et_deliver_fee.getText().toString().trim(), info.getDeliver_fee(), title);
            info.setDeliver_fee(et_deliver_fee.getText().toString().trim());
        }
        if (isNull(et_other_fee)) {
        }else {
            String title = "其它费";
            change(title, et_other_fee.getText().toString().trim(), info.getOther_fee(), title);
            info.setOther_fee(et_other_fee.getText().toString().trim());
        }
        if (isNull(et_trade_agency)) {
        }else {
            String title = "代收货款";
            change(title, et_trade_agency.getText().toString().trim(), info.getTrade_agency(), title);
            info.setTrade_agency(et_trade_agency.getText().toString().trim());
        }
        if (isNull(et_is_recept)) {
        }else {
            String title = "回单份数";
            change(title, et_is_recept.getText().toString().trim(), info.getIs_recept(), title);
            info.setIs_recept( et_is_recept.getText().toString().trim());
        }

        applicant_message = et_sign_remark.getText().toString().trim();
        SignShipOrder signShipOrder = new SignShipOrder();
        signShipOrder.execute(info);
    }


    private void change(String title,String after,String before,String message){
        waybillAdjustmentHistoryInfo = new WaybillAdjustmentHistoryInfo();
        waybillAdjustmentHistoryInfo.setWaybill_adjustment_tittle(title);
        waybillAdjustmentHistoryInfo.setWaybill_adjustment_after(after);
        waybillAdjustmentHistoryInfo.setWaybill_adjustment_before(before);
        waybillAdjustmentHistoryInfo.setWaybill_adjustment_message(message+"：由"+ before + "变成" + after);
        infoList.add(waybillAdjustmentHistoryInfo);
    }
    public class SignShipOrder extends AsyncTask<ShippingOrderInfo, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {
            mProgressDialog = ProgressDialog.show(mContext, null, "正在加载中...");
            mProgressDialog.setCancelable(true);
        }

        protected String doInBackground(ShippingOrderInfo... params) {
            Map<String, Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("userInfo", userInfo);
            map.put("orderData", params[0]);
            map.put("applicant_message", applicant_message);
            map.put("applicant_type", 1);
            map.put("changeMessage", infoList);
            map.put("shiping_order_num",shopping_num );

            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appAddWaybillAdjustmentInfo"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            mProgressDialog.dismiss();
            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {

                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else {
                    FileUtil.getInstance().noUpload();
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    T.showShort(mContext, successMsg);
                    Intent mIntent = new Intent();
                    //把返回数据存入Intent
                    mIntent.putExtra("result", 1);
                    //设置返回数据
                    setResult(1, mIntent);
                    finish();
                }
            }
        }
    }


}
